package com.hera.base.web.vaadin;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import com.hera.base.util.Inject;
import com.hera.base.util.VaadinUtil;
import com.hera.base.util.exception.ManejadorExcepcionMensaje;
import com.hera.base.web.vaadin.push.BroadcastVaadinListener;
import com.hera.base.web.vaadin.push.BroadcasterVaadin;
import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Component;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;

@Push
@SuppressWarnings("serial")
@Theme("admin")
public class AplicationUI extends UI implements BroadcastVaadinListener{

	

	private static Logger log = Logger.getLogger(AplicationUI.class);

	@Autowired
	private MessageSource messageSource;

	Window window;
	private String keySession = null;

	@Override
	protected void init(VaadinRequest request) {
		Inject.inject(this);
//		Permiso permiso = (Permiso) VaadinSession.getCurrent().getAttribute("USUARIO");
		Component component=null;
//		if (permiso == null) {
//			component=new PortalLogin();
//		} else {
//			component=new PortalPlantilla();
//		}
		keySession = request.getWrappedSession().getId();
		component=new PortalLogin(this, keySession);
		VaadinUtil.aplicarReflexionId(component);
		setContent(component);
		UI.getCurrent().setErrorHandler(new ManejadorExcepcionMensaje(messageSource, this.getLocale()));
		
		//BroadcasterVaadin.register("u00001", keySession, this);
	}

	@Override
	public void receiveBroadcast(final String message) {
		// TODO Auto-generated method stub
		access(new Runnable() {
            @Override
            public void run() {            	
            	Notification.show("Alerta",
                        "Detalle de la Alerta",
                        Notification.Type.TRAY_NOTIFICATION);
            	
            }
        });
	}
	
	@Override
	public void receiveBroadcast() {
		// TODO Auto-generated method stub
		access(new Runnable() {
            @Override
            public void run() {            	
            	Notification.show("Alerta",
                        "Detalle de la Alerta",
                        Notification.Type.TRAY_NOTIFICATION);            	
            	System.out.println((PortalPlantilla)getContent());
            }
        });
	}

}
